package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.mapper.PjqsMapper;
import cn.e4j.modular.crm.entity.MmrdT;
import cn.e4j.modular.crm.entity.MmrsT;
import cn.e4j.modular.crm.entity.MmrvT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * Created by wenchuan on 2018/4/18.
 * <p>
 * 配件签收
 */

@Service
public class PjqsService{


    @Autowired
    PjqsMapper pjqsMapper;

    /**
     * 获取-已签收配件
     * <p>
     * 已签收的也先全部人员都可以看 （按仓库归属组织来限定）
     *
     * @param inDto
     * @return
     */
    public List<Dto> getPjqsOffList(Page<Object>page,Dto inDto) {
        return pjqsMapper.pjqsOffListPage(page,inDto);
    }


    /**
     * 获取-未签收配件
     * <p>
     * 已签收的也先全部人员都可以看 （按仓库归属组织来限定）
     *
     * @param inDto
     * @return
     */
    public List<Dto> pjqsOnList(Page<Object>page,Dto inDto) {
        return pjqsMapper.pjqsOnListPage(page,inDto);
    }

    /**
     * 签收配件保存
     *
     * @param
     * @return
     */
    @Transactional
    public void pjqsSave(SysUser currentUser,Dto inDto) {
        Date date = new Date();
        JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
        JSONArray characteristics = inJson.getJSONArray("CHARACTERISTICS");
        String mmrvdocno = "PJQS" + DateUtils.getDate("yyyyMMddHHmmssSSS");
        for (int i = 0; i < characteristics.size(); i++) {
            JSONObject dto = characteristics.getJSONObject(i);
            MmrvT mmrv_tPO = new MmrvT();
            mmrv_tPO.setMMRVENT(currentUser.getDBC());
            mmrv_tPO.setMMRVDOCNO(mmrvdocno);
            mmrv_tPO.setMMRVSEQ(BigDecimal.valueOf(i + 1));
            mmrv_tPO.setMMRVDOCDATE(dto.getString("MMRVDOCDATE"));
            mmrv_tPO.setMMRV001(dto.getString("MMRV001"));
            mmrv_tPO.setMMRV002(dto.getBigDecimal("MMRV002"));
            mmrv_tPO.setMMRV003(dto.getString("MMRV003"));
            mmrv_tPO.setMMRV004(dto.getString("MMRV004"));
            mmrv_tPO.setMMRV005(dto.getBigDecimal("MMRV005"));
            mmrv_tPO.setMMRV006(dto.getString("MMRV006"));
            mmrv_tPO.setMMRV007(dto.getString("MMRV007"));
            mmrv_tPO.setMMRV008(dto.getString("MMRV008"));
            mmrv_tPO.setMMRV009(dto.getString("MMRV009"));
            mmrv_tPO.setMMRV010(dto.getString("MMRV010"));
            mmrv_tPO.setMMRV011(dto.getString("MMRV011"));
            mmrv_tPO.setMMRV012(dto.getString("MMRV012"));
            mmrv_tPO.setMMRVACTI("Y");
            mmrv_tPO.setMMRVUSER(currentUser.getID());
            mmrv_tPO.setMMRVGRUP(currentUser.getORG_ID());
            mmrv_tPO.setMMRVORIU(currentUser.getID());
            mmrv_tPO.setMMRVORIG(currentUser.getORG_ID());
            //mmrv_tPO.setMMRVDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
            mmrv_tPO.setMMRVDATE(LocalDate.now());
            mmrv_tPO.setMMRVTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
            mmrv_tPO.insert();
            MmrsT mmrs_tPO = new MmrsT();
            mmrs_tPO.setMMRSENT(currentUser.getDBC());
            mmrs_tPO.setMMRS001(dto.getString("MMRS001"));
            mmrs_tPO.setMMRS002(dto.getString("MMRS002"));
            mmrs_tPO.setMMRS003(dto.getString("MMRS003"));
            mmrs_tPO.setMMRS004(dto.getString("MMRS004"));
            mmrs_tPO.setMMRS005(dto.getString("MMRS005"));
            mmrs_tPO.setMMRS006(dto.getString("MMRS006"));
            int MMRS008 = dto.getInteger("MMRS008")- dto.getInteger("MMRV005");
            mmrs_tPO.setMMRS008(BigDecimal.valueOf(MMRS008));
            //WHERE  mmrsent = #{mmrsent} AND mmrs001 = #{mmrs001} AND mmrs002 = #{mmrs002}
            // AND mmrs003 = #{mmrs003} AND mmrs004 = #{mmrs004} AND mmrs005 = #{mmrs005}
            // AND mmrs006 = #{mmrs006}
            UpdateWrapper<MmrsT> wrapper = new UpdateWrapper<>();
            wrapper.eq("MMRSENT",mmrs_tPO.getMMRSENT());
            wrapper.eq("MMRS001",mmrs_tPO.getMMRS001());
            wrapper.eq("MMRS002",mmrs_tPO.getMMRS002());
            wrapper.eq("MMRS003",mmrs_tPO.getMMRS003());
            wrapper.eq("MMRS004",mmrs_tPO.getMMRS004());
            wrapper.eq("MMRS005",mmrs_tPO.getMMRS005());
            wrapper.eq("MMRS006",mmrs_tPO.getMMRS006());
            mmrs_tPO.update(wrapper);
        }
    }


}
